<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">员工姓名：</div>
      <el-input v-model="from.name"  clearable placeholder="请输入内容" class="search-main" />
      <el-button type="primary" @click="query">查询</el-button>
    </div>
    <div class="create-container">
      <el-button type="primary" @click="popup('add')">添加员工</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="employeeList">
        <el-table-column type="index" label="序号" />
        <el-table-column label="员工姓名"  prop="name" />
        <el-table-column label="登录账号"  prop="userName" />
        <el-table-column label="联系方式"  prop="phonenumber" />
        <el-table-column label="角色"  prop="roleName" />
        <el-table-column label="状态">
          <template #default="scope">
            {{ scope.row.status }}
          </template>
        </el-table-column>
        <el-table-column label="添加时间" prop="createTime" />
        <el-table-column label="操作" fixed="right" width="220">
          <template #default="{row}">
            <el-button size="mini" type="text" @click="popup('edit',row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="del(row.id)">删除</el-button>
            <el-button size="mini" type="text">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--  分页 -->
    <div class="page-container">
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      ></el-pagination>
    </div>
    <!-- 添加员工 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%">
        <el-form ref="fromList" :model="addfrom" :rules="rules" >
          <el-form-item label="员工姓名" prop="name">
            <el-input v-model="addfrom.name" placeholder="请填写员工姓名"></el-input>
          </el-form-item>
          <el-form-item label="登录账号" prop="userName">
            <el-input v-model="addfrom.userName" placeholder="请填写登录账号"></el-input>
          </el-form-item>
          <el-form-item label="联系方式" prop="phonenumber">
            <el-input v-model="addfrom.phonenumber" placeholder="请填写联系方式"></el-input>
          </el-form-item>
          <div>默认密码:  123456</div>

          <!-- // ! 选择角色 功能不完善  -->
          <el-form-item label="分配角色" prop="roleName"> <br>
            <el-select v-model="addfrom.roleName" placeholder="请选择分配角色">
              <el-option
                v-for="item in list"
                :key="item.id"
                :label="item.id"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="员工状态" prop="status"> <br>
            <el-radio v-model="addfrom.status" label="0">禁用</el-radio>
            <el-radio v-model="addfrom.status" label="1">启用</el-radio>
          </el-form-item>
        </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addition">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addUserlist, delList, editUserlist, getUserlist, particlist } from '@/api/SysUser'
export default {
  name: 'Employee',
  data() {
    return {
      total:0,  //共多少页
      employeeList: [],
      from:{
        name:null,
        page:1,
        pageSize:10
      },
      dialogVisible:false,  //弹框
      title:'',  // 弹窗标题
      num : '',//区分编辑还是 添加
      /* 添加数据 */
      addfrom:{
        name:null,
        userName:null,
        roleId:3,  //  用户 id
        roleName:null,
        status:0,  //  状态
        phonenumber:null
      },
      /*  修改获取到的数据 */
      editFrom:{},
      /* 角色权限 */
      list:[{id:'基础数据管理'},{id:'行车管理'},{id:'员工及系统管理'}],
      rules:{
        name:[
           { required: true, message: '请填写员工姓名', trigger: 'blur' },
        ],
        userName:[
           { required: true, message: '请填写登录账号', trigger: 'blur' },
        ],
        phonenumber:[
           { required: true, message: '请填写联系方式', trigger: 'blur' },
           { pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/,message:'请输入正确的手机号',trigger:'change' }
        ],
        status:[
           { required: true, message: '请选择员工状态', trigger: 'blur' }
        ],
      }
    }
  },
  created(){
    this.getList()
  },
  methods:{
    async getList(){
     const {data,data:{rows}} = await getUserlist(this.from)
     this.employeeList = rows
     this.total = data.total
     },
     /*  搜索 */
    query(){
      this.getList()
    },
      /* 添加员工 */
     async popup(prev,id){
        this.num = prev
        if(prev === 'add'){
          this.title = '添加员工'
          this.$refs.fromList.resetFields()
        } else {
          this.title = '编辑员工'
          const {data} = await particlist(id)
          this.editFrom = data
          this.addfrom = data
        }
        this.dialogVisible = true
      },
      /* 确认添加 */
     async addition(){
      /*  兜底校验 */
       await this.$refs.fromList.validate()
        this.dialogVisible = false
        if (this.num === 'add'){
          /*   添加角色 */
           await addUserlist(this.addfrom)
           this.getList()
        } else {
          /*  修改角色 */
           await editUserlist(this.editFrom)
            this.getList()
        }
      },
     /* 每页多少条数据 */
     handleSizeChange(val){
       this.from.pageSize = val
       this.getList()  
     },
     /*  切换分页 (到指定页数) */
     handleCurrentChange(page){
       this.from.page = page
       this.getList()
     },

/* 删除 */
      async del(id){
       await delList(id)
       this.getList()
     }
  }
}
</script>

<style lang="scss" scoped>
.employee-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  ;
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
::v-deep .el-dialog__body {
    padding: 30px 90px;
}
</style>